/*
	This file is part of SRServerLibrary.
	Foobar is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	SRServerLibrary is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with SRServerLibrary.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "MBItem.h"
#include "sys/stat.h"
#include <dirent.h>
#include <iostream>


using namespace std;



MBItem::MBItem(string itemPath, string itemName)
{	
	// build the absolute path to this item
	absolutePath = itemPath;
	//if(strcomp(absolutePath.at(strlen(absolutePath) - 1), "/") == 0)
	if(itemName != "")
	{
		absolutePath = absolutePath + "/" + itemName;
	}
	
	// check whether this item is a directory or not
	struct stat thisItemStat;
	stat((const char*)absolutePath.c_str(), &thisItemStat);
	if(S_ISDIR(thisItemStat.st_mode)) isDirectoryItem = true;
	else isDirectoryItem = false;
	
	// set default variable values
	loaded = false;
}


bool MBItem::isDirectory()
{
	return isDirectoryItem;
}


bool MBItem::isFile()
{
	return !isDirectoryItem;
}


string MBItem::getAbsolutePath()
{
	return absolutePath;
}


vector<MBItem> MBItem::getDirListing()
{
	return dirListing;
}


MBItem MBItem::getItemAt(int index)
{
	return dirListing.at(index);
}


void MBItem::load()
{
	if(!isFile()) // no need to load files
	{
		const char *dir = getAbsolutePath().c_str(); // for now
		struct dirent *entry;
		DIR *d;
	
		char *currentItem;
		struct stat currentItemStat;
	

		if (!(d = opendir(dir)))
		{
			cout << "error: could not open directory";
			// TODO: log!
		}
	
		while (entry = readdir(d))
		{
			if (entry->d_name[0] == '.')
			{
				cout << "ignoring item " << entry->d_name << endl;
				continue;
			}
		
			// check whether the item is a directory or not
			currentItem = (char*)malloc(strlen(dir)+strlen(entry->d_name)+2);
			if(currentItem == NULL)
			{
				cout << "COULD NOT ALLOCATE MEMORY :(:(" << endl;
				// TODO: log!
			}
		
			currentItem[0] = '\0';
			strcpy(currentItem, dir);
			strcat(currentItem, "/");
			strcat(currentItem, entry->d_name);
			cout << "currentItem: " << currentItem << endl;
		
			stat((const char*)currentItem, &currentItemStat);
		
			// create MBItem objects here
			dirListing.push_back(MBItem(dir, entry->d_name));
			
			
			cout << endl;
		
			// free memory
			free(currentItem);
		}
	
		closedir(d);
	}
}

string MBItem::open()
{
	if(isFile()) // it is a file. open it
	{
		string playString = "play " + getAbsolutePath();
		
		return playString; // TODO: should not be returned, but broadcasted to the other plugins
	}
	
	if(!this->isLoaded()) // has not been loaded yet
	{
		this->load(); // load!
		loaded = true;
	}
	
	return getDirListingAsString();
}


string MBItem::asString()
{
	return absolutePath;
}


string MBItem::getDirListingAsString()
{
	string stringListing;
	
	vector<MBItem>::iterator itDirListing;
	for(itDirListing = dirListing.begin(); itDirListing != dirListing.end(); itDirListing++)
	{
		MBItem currentItem = *(itDirListing);
		stringListing = stringListing + currentItem.asString() + "\n";
	}
	
	
	cout << "dirListing contains " << dirListing.size() << " elements" << endl;
	
	return stringListing;
}


int MBItem::itemCount()
{
	return dirListing.size();
}


bool MBItem::isLoaded()
{
	return loaded;
}
